home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 248 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.0 KB  |  62 lines

  1. Path: saul3.u.washington.edu!rmariano
  2. From: Ramon Mariano Jr <rmariano@u.washington.edu>
  3. Newsgroups: comp.std.c
  4. Subject: HELP!! Beginner's question...
  5. Date: Wed, 31 Jan 1996 00:57:49 -0800
  6. Organization: University of Washington
  7. Message-ID: <Pine.OSF.3.91l.960131005708.7552A-100000@saul3.u.washington.edu>
  8. NNTP-Posting-Host: saul3.u.washington.edu
  9. Mime-Version: 1.0
  10. Content-Type: TEXT/PLAIN; charset=US-ASCII
  11. NNTP-Posting-User: rmariano
  12.  
  13. Hello everybody!
  14.  
  15. I'm a beginning C-programmer and for the past couple of days, I've been
  16. totally stuck in my program. The purpose of my program is to ask the user
  17. for an integer 'i' and raise it by a power 'n'. I've been succesful in
  18. computing 5 to the 6th power, 3 to the 4th power, and other small-sized
  19. equations. However, when I try to raise 5 to the 7th power, I get "12589" 
  20. when it really should be "78125". My teacher told me that instead of using
  21. plain 'int', I should use 'long int'. I tried that, but it's still not
  22. working. If anyone can help me out here, I'd GREATLY appreciate it! I've
  23. included my code below... (by the way, I'm not allowed to use the <math.h>
  24. library)
  25.  
  26.  
  27. ------------------------------------------------------------------------
  28.  
  29. #include <stdio.h>
  30.                   
  31. int main(void)
  32. {
  33.    int i,         /* integer */
  34.        n,         /* power to raise integer */
  35.        i_total,   /* integer's total value*/
  36.        count;     /* keeps track of number of loops run */
  37.  
  38.    /* asks for an integer and stores it in 'i' */
  39.    printf("Enter a non-negative integer: ");
  40.    scanf("%d", &i); 
  41.  
  42.    /* asks for a power and stores it in 'n' */
  43.    printf("What power should we raise it to? ");
  44.    scanf("%d", &n);
  45.  
  46.    /* if 'n' is negative, program will treat it as a '0' */    
  47.    if (n < 0){
  48.        n = 0;
  49.    }            
  50.                     
  51.     i_total = 1;       /* gives i_total an initial total of '1' */
  52.  
  53.     for (count = 1;  count <= n;  count = count + 1){
  54.         i_total = i_total * i;
  55.     }
  56.  
  57.     /* prints out the inputs and result */
  58.     printf("%d raised to the %dth power is %d\n\n", i, n, i_total); 
  59.  
  60.     return(0);    
  61. }
  62.